import { GetProps, InputNumber } from 'antd'
import { CustomCellEditorProps } from '@ag-grid-community/react'

import styles from './styles.module.less'

type TInputNumberProps = GetProps<typeof InputNumber>

interface IInputNumberCellEditorProps extends CustomCellEditorProps<any, number | string> {
  inputNumberProps?: Omit<TInputNumberProps, 'size' | 'value' | 'onChange'>
}

export const InputNumberCellEditor = Object.assign(
  ({ api, value, onValueChange, inputNumberProps }: IInputNumberCellEditorProps) => {
    return (
      <InputNumber
        {...inputNumberProps}
        className={styles.InputNumberCellEditor}
        value={value as any}
        onChange={val => onValueChange(val as any)}
        onBlur={() => api.stopEditing()}
      />
    )
  },
  {
    defineProps: (
      props: Pick<IInputNumberCellEditorProps, 'inputNumberProps'>,
    ): Pick<IInputNumberCellEditorProps, 'inputNumberProps'> => props,
  },
)
